package cn.anecansaitin.not_enough_harness.common.capability.harness_capability;

import net.minecraft.network.chat.Component;
import net.minecraft.world.SimpleMenuProvider;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.animal.HappyGhast;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.CraftingMenu;
import net.minecraft.world.item.ItemStack;

public class CraftingTableHarnessHandler implements HarnessCapabilityHandler{
    public static final CraftingTableHarnessHandler INSTANCE = new CraftingTableHarnessHandler();

    @Override
    public void tick(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
    }

    @Override
    public void use(ItemStack harness, HappyGhast happyGhast, LivingEntity driver) {
        if (happyGhast.level().isClientSide() || !(driver instanceof Player player)) {
            return;
        }

        player.openMenu(new SimpleMenuProvider((id, inventory, player1) -> new CraftingMenu(id, inventory, ContainerLevelAccess.create(player1.level(), player1.blockPosition())){
            @Override
            public boolean stillValid(Player player) {
                return player.position().distanceToSqr(happyGhast.position()) < 100;
            }
        }, Component.empty()));
    }
}
